<template>
  <div class="app-container">
    <el-tag color="#2b85e4" style="color:white;margin-bottom:20px" size="large">
      组信息
    </el-tag>
    <el-row :gutter="30">
      <el-col :span="24">
        <el-table
          :data="formGroupTableData"
          border
          empty-text="暂无数据"
          style="width: 100%">
          <el-table-column
            label="组编码"
            property="code"
            width="200">
          </el-table-column>
          <el-table-column
            label="组名称"
            property="name"
            width="150">
          </el-table-column>
          <el-table-column
            label="组描述"
            property="description">
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <br/>
    <br/>
    <el-tag color="#2b85e4" style="color:white;margin-bottom:20px" size="large">
      配置项列表
    </el-tag>
    <el-row :gutter="30">
      <el-col :span="24">
        <div>
          <el-button icon="el-icon-plus" type="primary" size="mini" @click="addData">新增</el-button>
          <el-button icon="el-icon-close" type="danger" size="mini" @click="deleteAny">删除</el-button>
        </div>
        <div class="query-form">
          <el-form ref="tableSearchParam" :model="tableSearchParam" label-width="50px" :inline="true">
            <el-form-item label="配置项编码" label-width="90px">
              <el-input v-model="tableSearchParam.code" style="width:200px" size="small" clearable></el-input>
            </el-form-item>
            <el-form-item label="配置项名称" label-width="90px">
              <el-input v-model="tableSearchParam.name" style="width:200px" size="small" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="listTableData" icon="el-icon-search" size="small">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <br/>
        <el-table
          :data="tableData"
          border
          empty-text="暂无数据"
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="35">
          </el-table-column>
          <el-table-column
            label="配置项编码"
            property="code"
            width="150">
          </el-table-column>
          <el-table-column
            label="配置项名称"
            property="name">
          </el-table-column>
          <el-table-column
            property="createdTime"
            label="创建时间"
            width="150">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="success"
                @click="editData(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-close"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=this.currentPage
            :page-sizes="[10, 20, 30, 40]"
            :page-size=this.pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total=this.total>
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="actionType=='add'?'新增配置项':'修改配置项'" :visible.sync="dialogFormVisible" top="10vh" width="60%">
      <div style="height:400px;overflow-y: auto">
        <el-form :model="formData" style="width:400px" :rules="operationRules" ref="operationRuleForm">
          <el-row :gutter="20" style="width:610px">
            <el-col :span="12">
              <el-form-item label="配置项编码" :label-width="formLabelWidth" prop="code">
                <el-input v-model="formData.code" auto-complete="off" style="width:200px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="配置项名称" :label-width="formLabelWidth" prop="name">
                <el-input v-model="formData.name" auto-complete="off" style="width:200px"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" style="width:610px">
            <el-col :span="12">
              <el-form-item label="配置项类型" :label-width="formLabelWidth" prop="controlCode">
                <el-select v-model="formData.controlCode" placeholder="请选择" no-data-text="暂无数据" style="width:200px"
                           @change="controlChange">
                  <el-option
                    v-for="item in allControlData"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序码" :label-width="formLabelWidth">
                <el-input v-model="formData.itemSort" auto-complete="off" style="width:200px"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" style="width:610px">
            <el-col :span="24">
              <el-form-item label="" :label-width="formLabelWidth">
                <el-row v-for="attrItem in allAttrData" :gutter="30" :key="attrItem.value">
                  <el-col :span="24" v-if="controlSelect.indexOf(attrItem.code)!=-1">
                    {{attrItem.name}}：
                    <el-radio v-model="attrItem.value" :label="vItem"
                              v-for="vItem in (attrItem.description.split(';'))" :key="vItem.value">{{vItem}}
                    </el-radio>
                  </el-col>
                  <el-col :span="24" v-if="controlText.indexOf(attrItem.code)!=-1">
                    {{attrItem.name}}：
                    <el-input type="textarea" v-model="attrItem.value" auto-complete="off" :rows="6"></el-input>
                    <span style="font-size: 12px">若有多个值，请用英文分号“;”进行隔开。</span>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="200">
            <el-col :span="24">
              <el-form-item label="验证规则" :label-width="formLabelWidth">
                <el-input v-model="formData.verifyRule" auto-complete="off" type="textarea" :rows="6"
                          width="500"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="200">
            <el-col :span="24">
              <el-form-item label="描述" :label-width="formLabelWidth" prop="url">
                <el-input v-model="formData.description" auto-complete="off" type="textarea" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd" v-if="actionType=='add'" :loading="loading">保存</el-button>
        <el-button type="primary" @click="handleEdit" v-else :loading="loading">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss">
  .page {
    text-align: center;
    margin-top: 20px;
  }

  .query-form {
    border-bottom: 1px solid #ebeef5;
    padding-top: 25px;
  }

  .el-textarea__inner {
    width: 500px
  }
</style>

<script>
  import controlApi from '@/api/controlApi'
  import controlAttrApi from '@/api/controlAttrApi'
  import formGroupApi from '@/api/formGroupApi'
  import formItemApi from '@/api/formItemApi'
  import formGroupRItemApi from '@/api/formGroupRItemApi'

  export default {
    created() {
      this.getFormGroupData()
      this.initData()
    },
    data() {
      return {
        handleControl: ['radio', 'select', 'checkbox'],
        controlSelect: ['type', 'mode', 'disable', 'isMust'],
        controlText: ['range', 'selectItems', 'url','baseUrl'],
        attrValue: {},
        allAttrData: [],
        allControlData: [],
        formGroupTableData: [],
        tableData: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableSearchParam: {
          code: '',
          name: ''
        },
        formGroupId: this.$route.query.id,
        dialogFormVisible: false,
        formLabelWidth: '100px',
        formData: {
          formItemType: '3',
          code: '',
          name: '',
          controlCode: '',
          verifyRule: '',
          description: '',
          templateId: '',
          groupId: '',
          itemAttr: '',
          itemSort: ''
        },
        loading: false,
        actionType: '',
        multipleSelection: [],
        operationRules: {
          code: [
            {required: true, message: '请输入配置项编码', trigger: 'blur'},
            {max: 50, message: '长度不超过50 个字符', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入配置项名称', trigger: 'blur'},
            {max: 50, message: '长度不超过50 个字符', trigger: 'blur'}
          ],
          controlCode: [
            {required: true, message: '请选择配置项类型', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      initData() {
        this.listTableData()
      },
      listTableData() {
        this.tableSearchParam.groupId = this.formGroupId
        formGroupRItemApi.listformGroupRItem(this.tableSearchParam, this.currentPage, this.pageSize, (res) => {
          this.tableData = res.list
          this.total = res.total
        }, () => {
        })
      },
      getFormGroupData() {
        this.formGroupTableData = []
        formGroupApi.getFormGroup(this.formGroupId, (res) => {
          this.formGroupTableData.push(res)
        }, () => {
        })
      },
      addData() {
        //清空历史值
        this.formData = {
          formItemType: '3',
          code: '',
          name: '',
          controlCode: '',
          verifyRule: '',
          description: ''
        }
        this.attrValue = {}
        this.allAttrData = []
        this.allControlData = []
        controlApi.listAllControl({}, (res) => {
          this.allControlData = res
          this.formData.groupId = this.formGroupId
          this.actionType = 'add'
          this.dialogFormVisible = true
        }, () => {
          //this.$refs['operationRuleForm'].resetFields()
        })
      },
      handleAdd() {
        this.setItemAttr()
        this.$refs['operationRuleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            formItemApi.addFormItem(this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })
      },
      setItemAttr() {
        this.formData.itemAttr = []
        for (var ad of this.allAttrData) {
          console.log(ad)
          const a = {
            key: ''
          }
          a.key = ad.code
          if (this.handleControl.indexOf(this.formData.controlCode) != -1 && ad.code != 'mode') {
            const newVal = ad.value.split(';')
            const selectValue = []

            newVal.forEach((item, i) => {
              const p = {
                id: '',
                name: ''
              }
              p.id = i
              p.name = item
              selectValue.push(p)
            })
            a.value = selectValue
          } else {
            a.value = ad.value
          }
          this.formData.itemAttr.push(a)
        }
      },

      setAllAttrData(code) {
        this.allAttrData = []
        for (var acd of this.allControlData) {
          if (acd.code == code) {
            this.allAttrData = acd.attr
            break;
          }
        }
      },
      editData(index, row) {
        //清空历史值
        this.formData = {
          code: '',
          name: '',
          controlCode: '',
          verifyRule: '',
          description: ''
        }
        let that = this
        that.actionType = 'edit'

        formGroupRItemApi.getformGroupRItem(row.id, (res1) => {
          that.formData = res1
          this.allControlData = res1.allControl


          this.setAllAttrData(res1.controlCode)

          that.formData.id = row.id
          this.formData.formItemType = '3'
          that.dialogFormVisible = true
        }, () => {

        })
      },
      handleEdit() {
        this.setItemAttr()

        this.$refs['operationRuleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            formItemApi.updateFormItem(this.formData.id, this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })
      },
      handleDelete(index, row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          formGroupRItemApi.deleteSingleformGroupRItem(row.id, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.listTableData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.listTableData()
      },
      searchTableData() {

      },
      handleSelectionChange(val) {
        this.multipleSelection = []
        for (var ms of val) {
          this.multipleSelection.push(ms.id)
        }
      },
      deleteAny() {
        if (this.multipleSelection.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择记录!'
          })
          return;
        }
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          formGroupRItemApi.removeformGroupRItem(this.multipleSelection, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      controlChange(value) {
        this.setAllAttrData(value)
      }
    }
  }
</script>
